import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { Project } from './project.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { PaginateResult } from 'src/interfaces/http.interface';
import * as marked from 'marked'

@Injectable()
export class ProjectService {

  constructor(
    @InjectRepository(Project)
    private readonly projectRepository: Repository<Project>,
  ){}

  public async get(id: number): Promise<Project>{
    const data = await this.projectRepository.findOne(
        id,
    )
    if(!data){
      throw new HttpException("ID错误", HttpStatus.BAD_REQUEST)
    }
    data.intro = marked(data.intro)
    return data
  }

  public async list(pageIndex = 1, pageSize = 10): Promise<PaginateResult<Project>>{
      const [list, count] = await this.projectRepository.findAndCount({
          skip: pageSize * (pageIndex - 1),
          take: pageSize,
      })
      return {
          list, 
          count, 
          pageIndex, 
          pageSize,
          pageCount: Math.ceil(count / pageSize)
      }
  }



}
